﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DGZfP.EventPublisher.Calendar
{
    public class EventByDataRow : IEvent
    {

        public DataRow Row { get; set; }
        Dictionary<string, string> ColumnsMapping { get; set; }

        public const string IdColumn = "Id";
        public const string TitleColumn = "Title";
        public const string ContentColumn = "Content";
        public const string LocationColumn = "Location";
        public const string StartColumn = "Start";
        public const string EndColumn = "End";
        public const string AllDayColumn = "AllDay";
        public const string Context = "Context";
        public const string ContextColumn = "ContextColumn";


        public static Dictionary<string, string> DefaultColumnsMapping
        {
            get
            {
                Dictionary<string, string> columnsMapping = new Dictionary<string, string>();
                columnsMapping[IdColumn] = "Id";
                columnsMapping[TitleColumn] = "Title";
                columnsMapping[ContentColumn] = "Content";
                columnsMapping[LocationColumn] = "Location";
                columnsMapping[StartColumn] = "Start";
                columnsMapping[EndColumn] = "End";
                columnsMapping[AllDayColumn] = "AllDay";
                columnsMapping[Context] = "";
                columnsMapping[ContextColumn] = "";
                return columnsMapping;
            }
        }



        public EventByDataRow(DataRow row)
        {
            Initialize(row, "", DefaultColumnsMapping);
        }

        public EventByDataRow(DataRow row, string context)
        {
            Initialize(row, context, DefaultColumnsMapping);
        }

        public EventByDataRow(DataRow row, string context, Dictionary<string, string> ColumnsMapping)
        {
            Initialize(row, context, ColumnsMapping);
        }

        private void Initialize(DataRow row, string context, Dictionary<string, string> ColumnsMapping)
        {
            this.Row = row;
            this.ColumnsMapping = ColumnsMapping;
            ColumnsMapping[Context] = context;
            if (context != "") ColumnsMapping[ContextColumn] = "";
        }

        private object n(object o)
        {
            if (o == DBNull.Value) return null; else return o;
        }

        #region IEvent Member

        public String ID
        {
            get
            {
                return Row[ColumnsMapping[IdColumn]].ToString();
            }
            set
            {
                if (Row.Table.Columns[ColumnsMapping[IdColumn]].DataType == typeof(System.Int32))
                {
                    if (value != string.Empty)
                        Row[ColumnsMapping[IdColumn]] = int.Parse(value);
                    else
                        Row[ColumnsMapping[IdColumn]] = 0;
                }
                else
                Row[ColumnsMapping[IdColumn]] = value;
            }
        }

        public string Title
        {
            get
            {
                string mapping = ColumnsMapping[TitleColumn];
                if (mapping.Length == 0)
                    return string.Empty;
                else
                    return (string)n(Row[ColumnsMapping[TitleColumn]]);
            }
            set
            {
                string mapping = ColumnsMapping[TitleColumn];
                if (mapping.Length > 0) Row[ColumnsMapping[TitleColumn]] = value;
            }
        }

        public string Content
        {
            get
            {
                string mapping = ColumnsMapping[ContentColumn];
                if (mapping.Length == 0)
                    return string.Empty;
                else
                    return (string)n(Row[ColumnsMapping[ContentColumn]]);
            }
            set
            {
                string mapping = ColumnsMapping[ContentColumn];
                if (mapping.Length > 0)
                    Row[ColumnsMapping[ContentColumn]] = value;
            }
        }

        public string ContextId
        {
            get
            {
                if (ColumnsMapping[ContextColumn] == string.Empty)
                    return ColumnsMapping[Context];
                else
                    return (string)Row[ColumnsMapping[ContextColumn]];
            }
            set
            {
                if (ColumnsMapping[ContextColumn] == string.Empty)
                    ColumnsMapping[Context] = value;
                else
                    Row[ColumnsMapping[ContextColumn]] = value;
            }
        }

        public string Location
        {
            get
            {
                string mapping = ColumnsMapping[LocationColumn];
                if (mapping.Length == 0)
                    return string.Empty;
                else
                    return (string)n(Row[ColumnsMapping[LocationColumn]]);
            }
            set
            {
                string mapping = ColumnsMapping[LocationColumn];
                if (mapping.Length > 0)
                    Row[ColumnsMapping[LocationColumn]] = value;
            }
        }

        public DateTime? StartDate
        {
            get
            {
                if (Row[ColumnsMapping[StartColumn]] == DBNull.Value)
                    return null;
                else
                    return (DateTime?)Row[ColumnsMapping[StartColumn]];
            }
            set
            {
                Row[ColumnsMapping[StartColumn]] = value;
            }
        }

        public DateTime? EndDate
        {
            get
            {

                if( Row[ColumnsMapping[EndColumn]] == DBNull.Value  )
                    return null;
                else
                {
                    DateTime? date = (DateTime?)Row[ColumnsMapping[EndColumn]];
                    if (AllDay && date.HasValue )
                    {
                        return date.Value.AddDays(1);
                    }
                    return date;
                }

            }
            set
            {
                Row[ColumnsMapping[EndColumn]] = value;
            }
        }

        public Boolean AllDay
        {
            get
            {
                string mapping = ColumnsMapping[AllDayColumn];
                if (mapping.StartsWith("\"") && mapping.EndsWith("\""))
                    return Boolean.Parse(mapping.Trim('\"'));
                else
                    return (Boolean)Row[ColumnsMapping[AllDayColumn]];
            }
            set
            {
                string mapping = ColumnsMapping[AllDayColumn];
                if (!(mapping.StartsWith("\"") && mapping.EndsWith("\"")))
                    Row[ColumnsMapping[AllDayColumn]] = value;
            }
        }
        #endregion
    }
}
